package com.future.mayi.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.future.baselib.utils.ToastUtils;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.view.photoview.PhotoView;


/**
 * Created by 周大虎 on 2017/3/3.
 */

public class PhotoFragment extends BaseFragment {
    private PhotoView photoView;

    private ProgressBar progressBar;


    private String url;


    private int position;
    private View.OnClickListener onClick;

    public void setData(String url, int position, View.OnClickListener onClick) {

        if (!TextUtils.isEmpty(this.url) && this.url.equals(url))
            return;
        this.url = url;
        this.position = position;
        this.onClick = onClick;

        if (photoView != null)
            photoView.setOnClickListener(onClick);
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_item_photo;
    }

    @Override
    protected void initView() {
        photoView = (PhotoView) findViewById(R.id.photo_view);
        progressBar = (ProgressBar) findViewById(R.id.pb_photo);
        if (onClick != null)
            photoView.setOnClickListener(onClick);
    }


    @Override
    protected void initListener() {
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(url) || photoView == null)
            return;

        Glide.with(getActivity()).load(url).listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
                progressBar.setVisibility(View.GONE);
                ToastUtils.shortToast(getActivity(), "加载图片失败");
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> target, boolean b, boolean b1) {
                progressBar.setVisibility(View.GONE);
                return false;
            }
        }).placeholder(R.color.transparent).into(photoView);

    }


    public int getPosition() {
        return position;
    }
}
